class MyPage extends BaseList {
    constructor(action, orderby, flag, $pagesize, $pagination, cokiePageSize, cokiePageIndex, cokieListResult, listUrl, delUrl) {
        super(action, orderby, flag, $pagesize, $pagination, cokiePageSize, cokiePageIndex, cokieListResult, listUrl, delUrl);
    }
    OnPageLoad() {
        // 回车直接进行搜索
        $("panel_search").bind('keyup', function (e) {
            let key = e.keyCode;
            if (key === 13) {
                _Instance.FetchForPage(1);
            }
        });
        $('#loading').hide();
    }
    SetCriterion(item) {
        let _list = [];
        let _styleNo = $.trim($("#StyleNoFilterValue").val()).split('/');
        for (let i = 0; i < _styleNo.length; i++) {
            let _item = {};
            _item['StyleNo'] = $.trim(_styleNo[i]);
            _list.push(_item);
        }
        item['StyleNoList'] = _list;
        item['StyleNo'] = $.trim($("#StyleNoFilterValue").val());
        return item;
    }
    GetFilter() {
        let item = super.GetFilter(0);
        // 设置条件
        item = this.SetCriterion(item);
        return JSON.stringify(item);
    }
    EmptyTableContent() {
        $("#tab_data01 tbody").empty();
    }
    SetTableContent(result) {
        $("#tab_data01 tbody").empty();

        let content_01 = '';
        this.rows = result.length;
        for (let i = 0; i < result.length; i++) {
            //rowFlag=0:明细行； =1：:小计行； =2：合计行
            if (parseInt(result[i].rowFlag) === 0) {
                content_01 += '<tr>';
                content_01 += '<td>' + formatNullData(result[i].styleNo) + '</td>';
                content_01 += '<td>' + formatNullData(result[i].supplierName) + '</td>';
                content_01 += '<td>' + formatNullData(result[i].materialName) + '</td>';
                content_01 += '<td>' + formatNullData(result[i].materialCode) + '</td>';
                content_01 += '<td>' + formatNullData(result[i].specification) + '</td>';
                content_01 += '<td style="text-align:center;">' + formatNullData(result[i].uomName) + '</td>';
                content_01 += '<td>' + formatNullData(result[i].materialColourName) + '</td>';
                content_01 += '<td>' + formatNullData(result[i].destination) + '</td>';
                let _poQuantity = result[i].poQuantity === null ? null : (result[i].poQuantity === 0 ? null : (result[i].poQuantity).toFixed(2));
                content_01 += '<td style="text-align:right;">' + formatNullData(result[i].unitPrice) + '</td>';
                content_01 += '<td style="text-align:right;">' + formatNullData(_poQuantity) + '</td>';
                content_01 += '<td style="text-align:right;">' + formatNullData(result[i].amount.toFixed(2)) + '</td>';
                let _poQuantityWithLoss = result[i].poQuantityWithLoss === null ? null : (result[i].poQuantityWithLoss === 0 ? null : (result[i].poQuantityWithLoss).toFixed(2));
                content_01 += '<td style="text-align:right;">' + formatNullData(_poQuantityWithLoss) + '</td>';
                if (result[i].amountWithLoss !== null) {
                    content_01 += '<td style="text-align:right;">' + formatNullData(result[i].amountWithLoss.toFixed(2)) + '</td>';
                } else {
                    content_01 += '<td style="text-align:right;">&nbsp;</td>';
                }
            }
            // 按供应商小计
            if (parseInt(result[i].rowFlag) === 1) {
                content_01 += '<tr style="background-color:#ffe4b5; color:#000;">';
                content_01 += '<td colspan="10" style="text-align:right; font-weight:bold; padding-right:10px;">（小&nbsp;&nbsp;&nbsp;&nbsp;计）</td>';
                content_01 += '<td style="text-align:right; font-weight:bold;">' + (result[i].amount === null ? '&nbsp' : result[i].amount.toFixed(2)) + '</td>';
                content_01 += '<td></td>';
                if (result[i].amountWithLoss !== null) {
                    content_01 += '<td style="text-align:right; font-weight:bold;">' + result[i].amountWithLoss.toFixed(2) + '</td>';
                } else {
                    content_01 += '<td style="text-align:right; font-weight:bold;">&nbsp;</td>';
                }
            }
            // 总计
            if (parseInt(result[i].rowFlag) === 2) {
                content_01 += '<tr style="background-color:#00f9fc; color:#000;">';
                content_01 += '<td colspan="10" style="text-align:right; font-weight:bold; padding-right:10px;">（合&nbsp;&nbsp;&nbsp;&nbsp;计）</td>';
                content_01 += '<td style="text-align:right; font-weight:bold;">' + (result[i].amount === null ? '&nbsp' : result[i].amount.toFixed(2)) + '</td>';
                content_01 += '<td></td>';
                content_01 += '<td style="text-align:right; font-weight:bold;">' + (result[i].amountWithLoss === null ? '&nbsp' : result[i].amountWithLoss.toFixed(2)) + '</td>';
            }
            content_01 += '</tr>';
            $("#tab_data01 tbody").append(content_01);
            content_01 = '';
        }
        setOneTableRowColor("tab_data01");

        this.AutoAdjustTableHeightForLines();   // 自适应高度设置
        $(window).resize();                     // 防止因为出现滚动条而造成画面显示问题
        $('#loading').hide();
    }
    //《按钮》搜索
    DoSearch() {
        if (formatNullInputValue($.trim($("#StyleNoFilterValue").val())) === null) {
            ShowErrorMessagePanel('款号检索条件不能空。');
            return false;
        } else {
            this.FetchForPage(1);
        }
    }
    AutoAdjustTableHeightForLines() {
        let h = $(window).height();
        let _rc = Math.floor((h - 127 - 25) / 25);
        let _dh = _rc * 25;
        let _tah = this.rows * 25;
        _dh = (_tah >= _dh) ? _dh : _tah;

        $("#div_content").css({"max-height": _dh, height: _dh});
    }
    TableToExcel(table, filename) {
        if (!table.nodeType)
            table = document.getElementById(table);
        let _content =
                '<table border="1">' +
                '    <caption>款采购成本统计表</caption>' +
                '    <thead>' +
                '        <tr>' +
                '            <td>款号</td>' +
                '            <td>供应商</td>' +
                '            <td>材料名称</td>' +
                '            <td>材料编码</td>' +
                '            <td>规格(毛幅)</td>' +
                '            <td>单位</td>' +
                '            <td>颜色</td>' +
                '            <td>发货目的地</td>' +
                '            <td>采购单价</td>' +
                '            <td>采购数量</td>' +
                '            <td>金额</td> ' +
                '            <td>采购数量(含允差)</td> ' +
                '            <td>采购金额(含允差)</td> ' +
                '        </tr>' +
                '    </thead>' +
                '    <tbody>' + table.getElementsByTagName("tbody")[0].innerHTML +
                '    </tbody></table>';
        // 包装成一个完整的HTML文档，设置charset为urf-8以防止中文乱码
        let html = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">' +
                '<head><meta http-equiv="content-type" content="application/vnd.ms-excel; charset=utf-8" />' +
                '<style type="text/css">' +
                '    td {mso-number-format:"\@";}' +
                '    caption {' +
                '        height:30pt; font-size:14.0pt; font-weight:700; text-align:center;' +
                '    }' +
                '</style>' +
                '</head><body>' + _content + '</body></html>';

        // 实例化一个Blob对象，其构造函数的第一个参数是包含文件内容的数组，第二个参数是包含文件类型属性的对象
        let blob = new Blob([html], {type: "application/vnd.ms-excel"});
        window.URL = window.URL || window.webkitURL;
        let link = window.URL.createObjectURL(blob);
        let a = document.createElement("a");
        a.download = filename;
        a.href = link;
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
    }
}

var _Instance = null;

//-----------------------------------------------------------------------------
// 页面初始化
//-----------------------------------------------------------------------------
function onPageReady() {
    $("#search_btn").hide(); // 无需搜索，隐藏搜索按钮
    // 设置数据补齐功能
    $("#StyleNoFilterValue").bigAutocomplete({
        url: parent.basePath + "/master/product/fetchSerialNoForAutoComplate.html"
    });
    _Instance = new MyPage(0, null, null, null, null, null, null, null,
            "/purchase/report/pocost_per_style/fetchAll.html",
            "");

    _Instance.OnPageLoad();
}

// 画面大小或者位置移动
function onPageResize() {
    if (_Instance !== null)
        _Instance.AutoAdjustTableHeightForLines();
}
